package com.example.wechatdaka.controller;


import com.example.wechatdaka.entity.User;
import com.example.wechatdaka.entity.UserRole;
import com.example.wechatdaka.service.UserRoleService;
import com.example.wechatdaka.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lx
 * @since 2020-11-05
 */
@RestController
@RequestMapping("/userrole")
public class UserRoleController {
    @Autowired
    private UserRoleService userRoleService;


    @RequestMapping(value = "/index",method = RequestMethod.POST)
    public Result queryAll(){
        List<UserRole> userRoleList = userRoleService.list(null);
        if (userRoleList!=null){
            System.out.println("userList:"+userRoleList.toString());
            return new Result(Result.SUCCESS,userRoleList,"success");
        }
        return new Result(Result.ERROR,null,"");
    }
    @RequestMapping(value = "/saveOrUpdate",method = RequestMethod.POST)
    public Result saveOrUpdateUser(@RequestBody UserRole userRole){
        System.out.println("user:"+userRole.toString());
        boolean save = userRoleService.saveOrUpdate(userRole);
        if (save){
            return new Result(Result.SUCCESS,null,"success");
        }
        return new Result(Result.ERROR,null,"error");
    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result updateUser(@RequestBody UserRole userRole){
        System.out.println("user:"+userRole.toString());
        boolean save = userRoleService.updateById(userRole);
        if (save){
            return new Result(Result.SUCCESS,userRole,"success");
        }
        return new Result(Result.ERROR,null,"");
    }
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Result dedleteUser(@RequestBody String id){
        System.out.println("user:"+id.toString());
        boolean save = userRoleService.removeById(id);
        if (save){
            return new Result(Result.SUCCESS,null,"success");
        }
        return new Result(Result.ERROR,null,"");
    }
}

